<?php


/**
 * 订单Model
 * Enter description here ...
 * @author Admin
 *
 */
class OrderModel extends CommonModel {
	
	/**
	 * 订单过期控制
	 * 
	 * Enter description here ...
	 */
	public function _updata(){
		$config = C('SYSCONFIG');
		$order_expired = time() - $config['order_expired'] * 86400;  //过期时间
		$map = array(
			'state' =>array('eq',0),
			'addtime'=>array('lt',$order_expired),
		);
		
		$this->where($map)->setField('state', 5);
	}
	
	
	
	/**
	 * (non-PHPdoc)
	 * @see CommonModel::getdata()
	 */
	public function getdata($map,$limit,$field = 'O.*',$type='auction'){
		$prefix = C('DB_PREFIX');
		if (is_array($field)){
			$field = implode(',',$field);
		}
    	$data = $this->Table("{$prefix}order as O")->
					  join("{$prefix}{$type} as A ON O.aid = A.id")->
					  join("{$prefix}goods as G ON A.gid = G.id")->
					  join("{$prefix}express as E ON O.id = E.oid")->
					  join("{$prefix}sunpic as S ON O.id = S.oid")->
					  field($field)->where($map)->
					  order('O.id desc')->limit($limit)->findAll();
		return $data;
	}
	
	
	
	
	/**
	 * 获取订单
	 * Enter description here ...
	 * @param unknown_type $map
	 * @param unknown_type $field
	 * @param unknown_type $type
	 */
	public function getOne($map,$field = 'O.*',$type='auction'){
		$prefix = C('DB_PREFIX');
		if (is_array($field)){
			$field = implode(',',$field);
		}
    	$data = $this->Table("{$prefix}order as O")->
					  join("{$prefix}{$type} as A ON O.aid = A.id")->
					  join("{$prefix}goods as G ON A.gid = G.id")->
					  join("{$prefix}accessory_relation as AR ON G.id = AR.relationid and AR.table='Goods'")->
					  join("{$prefix}accessory as AC on AR.accessoryid = AC.id")->
					  field($field)->where($map)->
					  find();
		return $data;
	}
	
	
	
	
	/**
	 * 获取失效订单
	 * Enter description here ...
	 * @param unknown_type $uid
	 */
	public function getLose($uid){
		$prefix = C('DB_PREFIX');
		$map = array(
			'O.uid' => array('eq',$uid),
			'O.state'=>array('eq',5)
		);

		$data = $this->Table("{$prefix}order as O")->
					  join("{$prefix}auction as A ON O.aid = A.id")->
					  field('O.*,A.sn,A.goodsprice,A.ordertime')->where($map)->
					  order('O.id desc')->findAll();
					  
		if (empty($data)){
			return false;
		}else {
			
			$auction_log = D('Auction_log');   //竞拍日志
			$logmap['uid']=array('eq',$uid);
			foreach ($data as &$value){
				$logmap['aid']=array('eq',$value['aid']);
				$bout = $auction_log->getCount($logmap);
				
				$auction_logdata = $auction_log->where($logmap)->field('sum(beat) as beatsum')->find();
				$value['beatnum'] = $auction_logdata['beatsum'];          //实体币总和
				$value['bout'] = $bout;               
				                    //次数
				unset($logmap['aid']);
				unset($auction_logdata);
				unset($count);
			}
			
		}
		
		return $data;
	}
	
	
	
	
	
	/**
	 * 订单号
	 * Enter description here ...
	 */
	public function produceSn(){
		do {
			$string = rand_string(8);
			$sn = 'PA'.$string.date('YmdHis');
			$map['ordersn'] = array('eq',$sn);
			
			$tmp = $this->where($map)->find();
		}while (!empty($tmp));
		
		return $sn;
	}

	
	
	
	
	
}
?>